(function(){
    var factory = {
        pollingTime: 3000,    //轮播时间间隔
        init: function(){
            var _this = this;
            _this.scrollLoadingPic();

            /*导航定位*/
            _this.navPosition();

            //首页banner焦点幻灯片
            _this.banner();

            //数字滚动
            _this.dataLoop();
            // 产品服务滚动
            _this.serviceLoop();

            //news轮播
            _this.newsLoop();

            //partner轮播
            _this.partnerLoop();

            //特色服务
            _this.speService();

            //返回顶部
            _this.goBack();

            //在线咨询或留言
            _this.onlineIM();
        },
        scrollLoadingPic:function () {
            $("img.lazy").lazyload();
        },
        navPosition: function(){    //导航定位
            $('.menu-list a.list-link').click(function(){
                var hash = $(this).attr('data-hash');
                if(hash){
                    var scrollTo = $(hash).offset().top;
                    if(hash = '#product'){
                        scrollTo = scrollTo + 60;
                    }
                    $('body,html').animate({scrollTop:scrollTo},500);
                }
            });
        },
        banner: function(){    //首页banner焦点幻灯片
            var _this = this;
            var opts = {
                'time': _this.pollingTime //轮播时间间隔
            };

            function plugin(elm, options){
                var _this = this;

                _this.$elm = $(elm);

                opts = $.extend(opts, options||{});

                _this.init();
            }

            plugin.prototype = {
                init: function(){
                    var _this = this;

                    opts.idx = 1;    //初始化几秒后显示第一张图

                    opts.lengths = _this.$elm.find(opts.btnBox).find(opts.btnDom).length; //长度

                    //视图事件监听
                    _this.handle();
                },
                handle: function(){
                    var _this = this;

                    function polling(){
                        if(opts.idx >= opts.lengths){
                            opts.idx = 0;
                        }

                        _this.$elm.find(opts.btnBox).find(opts.btnDom).eq(opts.idx).addClass(opts.activeClass).siblings().removeClass(opts.activeClass);

                        _this.$elm.find(opts.imgBox).find(opts.imgDom).eq(opts.idx).addClass(opts.activeClass).siblings().removeClass(opts.activeClass);

                        ++opts.idx;
                    }

                    function pollingTimer(){
                        return setInterval(polling, opts.time);
                    }

                    opts.timeHandle = pollingTimer();

                    _this.$elm.hover(function(){
                        clearTimeout(opts.timeHandle);
                    },function(){
                        opts.timeHandle = pollingTimer();
                    });

                    _this.$elm.find(opts.btnBox).find(opts.btnDom).hover(function(e){
                        opts.idx = $(this).index();

                        polling();

                        clearTimeout(opts.timeHandle);

                        e.stopPropagation();
                    });
                }
            };

            $.fn.hslide = function(options){
                return new plugin(this, options);
            };

            /*banner轮播*/
            $('.m-slide').hslide({
                'time': _this.pollingTime,
                'imgBox': '.slide-img',
                'imgDom': 'li',
                'btnBox': '.slide-btn',
                'btnDom': 'a',
                'activeClass': 'active'
            });
        },
        dataLoop: function(){
            var _this = this;
            function loop(data, dom){
                if($(dom).children('span').first().text() !== '0'){
                    $(dom).animate({'top':'-64px'},200,function(){
                        $(this).children('span').first().remove().clone(true).appendTo(this);
                        $(this).css({top: 0});
                        core(data, dom);
                    });
                }else {
                    core(data ,dom);
                }
            }
            function core(data ,dom){
                var times = 0;
                var inter = setInterval(function(){
                    times++;
                    if(times > data){
                        clearInterval(inter);
                        return;
                    }
                    $(dom).animate({top:'-64px'},200,function(){
                        $(this).children('span').first().remove().clone(true).appendTo(this);
                        $(this).css({top: 0});
                    });
                },200);
            }
            function loops(){
                setTimeout(function(){
                    loop(3,'#data_1');
                },200);
                setTimeout(function(){
                    loop(5,'#data_2');
                },400);
                setTimeout(function(){
                    loop(1,'#data_3');
                },200);
                setTimeout(function(){
                    loop(2,'#data_4');
                },400);
                setTimeout(function(){
                    loop(4,'#data_5');
                },600);
                setTimeout(function(){
                    loop(2,'#data_6');
                },200);
                setTimeout(function(){
                    loop(1,'#data_7');
                },400);
                setTimeout(function(){
                    loop(3,'#data_8');
                },600);
                setTimeout(function(){
                    loop(6,'#data_9');
                },200);
                setTimeout(function(){
                    loop(2,'#data_10');
                },400);
                setTimeout(function(){
                    loop(9,'#data_11');
                },600);
            }
            loops();
            var time = 0;
            var globalInter = setInterval(function(){
                if(time++ > 37){
                    clearInterval(globalInter);
                }else{
                    loops();
                }
            },8000);
        },
        serviceLoop: function(){
            var _this = this;
            function loop(dom){
                setInterval(function(){
                    $(dom).animate({top:'-24px'},1000,function(){
                        $(this).children('li:lt(2)').remove().clone(true).appendTo(this);
                        $(this).css({top: 0});
                    });
                },2000);
            }
            setTimeout(function(){
                loop('#service-loop-a');
            },400);
            setTimeout(function(){
                loop('#service-loop-b');
            },1300);
            setTimeout(function(){
                loop('#service-loop-c');
            },900);
            setTimeout(function(){
                loop('#service-loop-d');
            },2200);
            setTimeout(function(){
                loop('#service-loop-e');
            },1700);
            setTimeout(function(){
                loop('#service-loop-f');
            },2800);
        },
        newsLoop: function(){ //news轮播
            var _this = this,
                index = 0,
                newsImgArr = $('.news-loop-img .news-img-wrap');
            var loop = function(){
                return setInterval(function(){
                    index++;
                    if(index >= newsImgArr.length){
                        index = 0;
                    }
                    $(newsImgArr[index]).addClass('news-active').siblings().removeClass('news-active');
                    $('.loop-btn a:eq('+index+')').addClass('loop-btn-active').siblings().removeClass('loop-btn-active');
                },_this.pollingTime);
            };
            var currLoop = loop();
            $('.loop-btn a').hover(function(e){
                if($(this).hasClass('loop-btn-active')){
                    return;
                }
                $(this).addClass('loop-btn-active').siblings().removeClass('loop-btn-active');
                clearInterval(currLoop);
                index = $(e.target).attr('data-index');
                $(newsImgArr[index]).addClass('news-active').siblings().removeClass('news-active');
                currLoop = loop();
            });
        },
        partnerLoop: function(){
            var _this = this,
                index = 0,
                currTimeout = '',
                partnerArr = $('.partner-outer-wrap');
            function loop(){
                index++ ;
                if(index >= partnerArr.length){
                    index = 0;
                }
                $('.partner-loop-btn a:eq('+index+')').addClass('partner-loop-btn-active').siblings().removeClass('partner-loop-btn-active');
                $('.partner-loop-content').animate({left:'-1170px'},500,function(){
                    $(this).children('.partner-outer-wrap').first().remove().clone(true).appendTo(this);
                    $(this).css({left: 0});
                    currTimeout = setTimeout(function(){
                        loop();
                    },_this.pollingTime);
                });
            }
            loop();

            $('.partner-loop-btn a').hover(function(e){
                if($(this).hasClass('partner-loop-btn-active')){
                    return;
                }
                $(this).addClass('partner-loop-btn-active').siblings().removeClass('partner-loop-btn-active');
                index = Number($(e.target).attr('data-index'));
                $('.partner-loop-content').children('.partner-outer-wrap:eq('+index+')').remove().clone(true).appendTo($('.partner-loop-content'));
                $('.partner-loop-content').children('.partner-outer-wrap:gt('+index+')').remove().clone(true).appendTo($('.partner-loop-content'));
                $('.partner-loop-content').children('.partner-outer-wrap:lt('+index+')').remove().clone(true).appendTo($('.partner-loop-content'));
            });
        },
        speService: function(){    //特色服务
            $('#special .m-special-btn a').hover(function(){
                var index = $(this).index();

                $(this).addClass('active').siblings().removeClass('active');
                if(index == 0){
                    $('#special .wrap-special-list').animate({'left':'0px'});
                }else if(index == 1){
                    $('#special .wrap-special-list').animate({'left':'-340px'});
                }
            });
        },
        goBack: function(){    //返回顶部
            $(window).scroll(function(){
                var scrollTop = $(window).scrollTop();
                if(scrollTop > 0){
                    $("#go-back").slideDown();
                }else{
                    $("#go-back").slideUp();
                }
            });
            // $(window).scroll();
            $("#go-back").on('click', function(){
                $(document).scrollTop(0);
                // $('body,html').animate({ scrollTop: 0 }, 500);
            });
        },
        onlineIM: function(){   //在线咨询

        },
        initIm: function(data){
            var html = '<li class="chatmsg chatmsg-left clearfix"><p className="service-time">'+ data.createTime +'</p><div class="service-msg"><div class="u-msg"><pre>欢迎您，请问有什么可以帮到您？</pre></div></div></li>';
            $('#m-im .im-msg ul').html(html);

            $('#m-im .im-enter textarea').val('');
        },
        showIM: function(){
            $('#m-im').slideDown();
        },
        hideIM: function(){
            $('#m-im').slideUp();
        },
        addImMsg: function(data, msg){
            var html = '<li class="chatmsg chatmsg-right clearfix">';
            html += '<p class="service-time">' + data.createTime + '</p>';
            html += '<div class="service-msg">';
            html += '<div class="u-msg"><pre>' + msg + '</pre></div>';
            html += '</div>';
            html += '</li>';

            $('#m-im .im-msg ul').append(html);
            $('#m-im .im-enter textarea').val('');
            $('#im-msg').scrollTop($('#im-msg')[0].scrollHeight);
        },
    };

    module.exports = factory;
})();
